package icl.exercise.utilities;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import com.google.android.maps.GeoPoint;

import android.location.Location;
import android.os.Environment;

public class FileUtilities {
	final static String _AppDirectoryPath = "/sdcard/ExerciseAid/";
	final static String _GPSfilename = "GPSData.csv";
	final String _AccelerometerFilename = "AccelerometerData.csv";

	public void ExportFile(String filename) {

		// check if there is a writable SD Card on the device
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state)) {
			// We can read and write the media
			mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
			// We can only read the media
			mExternalStorageAvailable = true;
			mExternalStorageWriteable = false;
		} else {
			// Something else is wrong. It may be one of many other states, but
			// all we need
			// to know is we can neither read nor write
			mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		if (mExternalStorageAvailable && mExternalStorageWriteable) {
			// create a File object for the parent directory
			File appDirectory = new File(_AppDirectoryPath);
			// have the object build the directory structure, if needed.
			appDirectory.mkdirs();
			// create a File object for the output file
			File outputFile = new File(appDirectory, filename);
			// now attach the OutputStream to the file object, instead of a
			// String
			// representation
			try {
				FileOutputStream fos = new FileOutputStream(outputFile);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void addNewAccelerometerValue(double x, double y, double z) {
		try {
			// Create file
			FileWriter fstream = new FileWriter(_AppDirectoryPath
					+ _AccelerometerFilename, true);
			BufferedWriter out = new BufferedWriter(fstream);

			DateFormat dateFormat = new SimpleDateFormat(
					"yyyy/MM/dd HH:mm:ss:SS");
			Date date = new Date();

			out.write(dateFormat.format(date) + "," + x + "," + y + "," + z
					+ "\n");

			// Close the output stream
			out.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addNewGPSValue(Location location) {

		try {
			// Create file
			FileWriter fstream = new FileWriter(_AppDirectoryPath
					+ _GPSfilename, true);
			BufferedWriter out = new BufferedWriter(fstream);

			DateFormat dateFormat = new SimpleDateFormat(
					"yyyy/MM/dd HH:mm:ss:SS");
			Date date = new Date();

			out.write(dateFormat.format(date) + "," + location.getLatitude()
					+ "," + location.getLongitude() + "\n");

			// Close the output stream
			out.close();

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static List<GeoPoint> GetGPSLocations() {

		try {
			List<GeoPoint> result = new ArrayList<GeoPoint>();

			File file = new File(_AppDirectoryPath + _GPSfilename);

			String line = null;

			BufferedReader bufRdr = new BufferedReader(new FileReader(file));

			while ((line = bufRdr.readLine()) != null) {
				String[] delimeteredLine = line.split(",");
				double lat = Double.parseDouble(delimeteredLine[1]);
				double lon = Double.parseDouble(delimeteredLine[2]);

				int latE6 = (int) (lat * 1e6);
				int lonE6 = (int) (lon * 1e6);

				GeoPoint temp = new GeoPoint(latE6, lonE6);
				result.add(temp);

			}

			return result;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}

	}
}
